package com.scl.calendar

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.scl.calendar.databinding.ActivityOpenBinding
import com.scl.reminder.CalendarDataBean
import com.scl.reminder.CalendarReminder
import com.scl.reminder.TypeStatus
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by cl om 2021/12/29
 */
class OpenActivity : AppCompatActivity() {

    private val mBinding by lazy {
        ActivityOpenBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)

        XXPermissions.with(this@OpenActivity)
            .permission(Permission.READ_CALENDAR)
            .permission(Permission.WRITE_CALENDAR)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    Toast.makeText(this@OpenActivity, "开启权限", Toast.LENGTH_SHORT).show()
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    Toast.makeText(this@OpenActivity, "拒绝权限", Toast.LENGTH_SHORT).show()
                }

            })

        mBinding.number.run {
            maxValue = 59
            minValue = 0
//            setOnValueChangedListener { _, i, i2 ->
//                Log.d("OpenActivity", "i:$i i2:$i2")
//
//                Log.d("OpenActivity", "选中：$i2")
//            }
        }

        mBinding.setNumber.setOnClickListener {
            mBinding.number.value = 30
        }

        mBinding.getNumber.setOnClickListener {
            Toast.makeText(this@OpenActivity, "现在的值:${mBinding.number.value}", Toast.LENGTH_SHORT)
                .show()
        }

        val list = ArrayList<CalendarDataBean>()

        list.add(CalendarDataBean(9, 0, "haha"))
        list.add(CalendarDataBean(10, 0, "haha"))
        list.add(CalendarDataBean(11, 0, "haha"))
        list.add(CalendarDataBean(13, 0, "haha"))
        list.add(CalendarDataBean(14, 0, "haha"))
        list.add(CalendarDataBean(14, 30, "haha"))
        list.add(CalendarDataBean(15, 0, "haha"))
        list.add(CalendarDataBean(16, 0, "haha"))

        mBinding.calendar.setOnClickListener {
            CalendarReminder.addCalendarList(
                context = this@OpenActivity,
                beanList = list,
                isNextDay = false,
                site_sign = "小鱼鱼提醒",
                description = "描述",
                reminderType = TypeStatus.NUMBER,
                number = 5,
                weekendReminder = true,
                openEndTime = hourToTimestamp(22),
                openStartTime = hourToTimestamp(8),
                isNoReminder = true,
                noStartTime = hourToTimestamp(13),
                noEndTime = hourToTimestamp(15)
            )
        }

        mBinding.delete.setOnClickListener {
            CalendarReminder.deleteCalendarEvent(
                context = this@OpenActivity,
                site_sign = "小鱼鱼提醒"
            )
        }

    }

    fun hourToTimestamp(hour: Int, minute: Int = 0): Long {
        val mCalendar = Calendar.getInstance()
        mCalendar.timeInMillis = System.currentTimeMillis()
        mCalendar.timeZone = TimeZone.getTimeZone("GMT+8")
        mCalendar.set(Calendar.HOUR_OF_DAY, hour)
        mCalendar.set(Calendar.MINUTE, minute)
        mCalendar.set(Calendar.SECOND, 0)
        mCalendar.set(Calendar.MILLISECOND, 0)
        return mCalendar.timeInMillis
    }
}